package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.AddressList;
import com.da.lon.wang.xlg.util.ViewHolder;

import java.util.List;

public class AddressListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<AddressList> addressLists;
    private String addidString;

    public AddressListAdapter(List<AddressList> list, Context context,
                              String addid) {

        inflater = LayoutInflater.from(context);
        this.addidString = addid;
        this.addressLists = list;

    }

    @Override
    public int getCount() {
        return addressLists != null ? addressLists.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return addressLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try {
            AddressList addressList = addressLists.get(position);

            convertView = inflater
                    .inflate(R.layout.ouka_address_listviewitem, null);

            TextView nameView = ViewHolder
                    .get(convertView, R.id.order_address_name);
            TextView phoneView = ViewHolder.get(convertView,
                    R.id.order_address_phone);
            TextView locationView = ViewHolder.get(convertView,
                    R.id.order_address_location);
            ImageView selectView = ViewHolder.get(convertView,
                    R.id.order_address_select);

            nameView.setText(addressList.getReal_name());
            phoneView.setText(addressList.getTelphone());
            if (addressList.getCounty_name() == null) {
                locationView.setText(addressList.getPro_name()
                        + addressList.getCity_name()
                        + addressList.getAddress());
            } else {
                locationView.setText(addressList.getPro_name()
                        + addressList.getCity_name() + addressList.getCounty_name()
                        + addressList.getAddress());
            }


            if (addidString.equals(addressList.getAdd_id())) {
                selectView.setVisibility(View.VISIBLE);
            } else {
                selectView.setVisibility(View.INVISIBLE);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }


        return convertView;
    }

}
